<?php
// +----------------------------------------------------------------------
// | Yzncms [ 御宅男工作室 ]
// +----------------------------------------------------------------------
// | Copyright (c) 2007 http://yzncms.com All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: fastadmin: https://www.fastadmin.net/
// +----------------------------------------------------------------------
namespace app\admin\controller;

use app\admin\model\CarModel;
use app\admin\model\College;
use app\admin\model\UserModel;
use app\common\controller\Adminbase;
use app\common\model\AliPay;
use app\common\model\BannerModel;
use app\common\model\InComemModel;
use app\common\model\MemberModel;
use app\common\model\RebateLog;
use PhpOffice\PhpSpreadsheet\IOFactory;
use think\Db;
use think\Exception;
use util\Tree;


class Income extends Adminbase
{

    protected function initialize()
    {
        parent::initialize();
        $this->modelClass = new \app\common\model\InComemModel();
    }

    public function index()
    {
        if ($this->request->isAjax()) {
            //如果发送的来源是Selectpage，则转发到Selectpage
            if ($this->request->request('keyField')) {
                return $this->selectpage();
            }
            list($page, $limit, $where, $sort, $order) = $this->buildTableParames();
            $list = $this->modelClass
                ->where($where)
                ->order($sort, $order)
                ->paginate($limit);

            $result = ["code" => 0, 'count' => $list->total(), "data" => $list->items()];
            return json($result);
        }
        return $this->fetch();
    }


    public function import()
    {
        $params = $this->request->param();
        if ($params['path']) {
            $path = (new Ajax())->upload('');
            $filePath = '.' . $params['path'];
            // 加载Excel文件
            $spreadsheet = IOFactory::load($filePath);

            $sheet = $spreadsheet->getActiveSheet();

            // 获取数据范围
            $info = $sheet->toArray();
            array_shift($info);

            try {
                $result = [];
                $no = [];
                foreach ($info as $key => $value) {
                    if ($key == 0) {
                        InComemModel::where(['day' => $value[0]])->delete();
                    }
                    if(!$value[2]){
                        continue;
                    }
                    //获取当前的uid
                    $user = (new \app\common\model\UserModel())->where(['zhu_id' => $value[2]])->select();
                    if (!$user) {
                        $no[] = $value[1] . '-' . $value[2];
                        continue;
                    }
                    foreach ($user as $k => $v) {
                        $data['uid'] = $v['id'];
                        $data['day'] = $value[0];
                        $data['username'] = $value[1];
                        $data['zid'] = $value[2];
                        $data['cid'] = $value[3];
                        $data['cate'] = $value[4];
                        $data['status'] = $value[5];
                        $data['scarcity'] = $value[6];
                        $data['stop_time'] = (int)$value[7];
                        $data['valid_time'] = (int)$value[8];
                        $data['level'] = $value[9];
                        $data['status1'] = $value[10];
                        $data['price'] = $value[11];
                        $data['is_ctb'] = $value[12];
                        $data['reason'] = $value[13];
                        $data['money'] = str_replace(',', '', $value[14]);
                        $data['create_time'] = time();
                        $result[] = $data;
                    }
                }

                Db::name('income')->insertAll($result);
                if ($no) {
                    return json([
                        'code' => 1,
                        'msg' => "共" . count($no) . "条数据没有上传成功" . "<br><br>" . implode("<br>", $no),
                    ]);
                }
                return json([
                    'code' => 1,
                    'msg' => '上传成功',
                ]);

            } catch (Exception $e) {
                return json([
                    'code' => 0,
                    'msg' => $e->getMessage(),
                ]);

            }


        }
    }

}
